﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;

namespace timetable
{
    [Serializable]
    public class EnergyTable : List<List<Dictionary<string, List<string>>>>
    {
        public readonly int CourseNumADay = 8;
        public double[,] valueTable; //用来存储每一节课的能量值
        public double sum; //存储最终总的能量值
        public TeacherAndClassroom teacherAndClassroom;
        public List<GroupList> CompareList;

        public EnergyTable(List<GroupList> resultGroupLists)
        {
            CompareList = new List<GroupList>();
            GroupList newList;
            foreach (GroupList list in resultGroupLists)
            {
                newList = new GroupList(list);
                CompareList.Add(newList);
            }
            teacherAndClassroom = new TeacherAndClassroom();
            valueTable = new double[5, CourseNumADay];
            // 初始化能量表
            List<Dictionary<string, List<string>>> day;
            Dictionary<string, List<string>> one;
            for (int i = 0; i < 5; i++)
            {
                day = new List<Dictionary<string, List<string>>>();
                for (int j = 0; j < CourseNumADay; j++)
                {
                    one = new Dictionary<string, List<string>>
                    {
                        {"语文", new List<string>()},
                        {"数学", new List<string>()},
                        {"英语", new List<string>()},
                        {"物理", new List<string>()},
                        {"化学", new List<string>()},
                        {"生物", new List<string>()},
                        {"政治", new List<string>()},
                        {"历史", new List<string>()},
                        {"地理", new List<string>()},
                        {"通技", new List<string>()},
                        {"体育", new List<string>()},
                        {"音乐", new List<string>()},
                        {"美术", new List<string>()}
                    };
                    day.Add(one);
                }

                Add(day);
            }

        }

        public void GetCourse()
        {
            
            foreach (GroupList groupList in CompareList)
            {

                foreach (Group @group in groupList.List)
                {
                    // 循环遍历每个分组的每节课程，并将其加入能量表中
                    for (int i = 0; i < 5; i++)
                    {
                        for (int j = 0; j < CourseNumADay; j++)
                        {
                            List<string> classList;
                            string className = group.Curriculum.table[i][j].Class;
                            if (className == null)
                                continue;

                            this[i][j].TryGetValue(className.Substring(0, 2), out classList);
                            if (!classList.Contains(className) && className != null)
                            {
                                classList.Add(className);
                            }

                        }
                    }

                }
            }
        }

        public string[] CalculateEnergy()
        {
            double maxValue = 0;
            string[] maxValueInfo = new string[3];
            double sum = 0;
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < CourseNumADay; j++)
                {
                    double value = 0;
                    int MainCouseSum = 0;

                    foreach (KeyValuePair<string, List<string>> keyValuePair in this[i][j])
                    {
                        Queue<int> teacherQueue;
                        teacherAndClassroom.TeacherDictionary.TryGetValue(keyValuePair.Key, out teacherQueue);
                        double teacherCount = teacherQueue.Count;
                        double newValue = keyValuePair.Value.Count / teacherCount * (keyValuePair.Value.Count / teacherCount);
                        if (value <= newValue)
                        {
                            value = newValue;
                            // 存储能量值最高的位置的信息
                            if (maxValue < value)
                            {
                                maxValueInfo[0] = i.ToString();
                                maxValueInfo[1] = j.ToString();
                                maxValueInfo[2] = keyValuePair.Key;
                                maxValue = value;
                            }
                        }
                    }
                    valueTable[i, j] = value;
                    
                    sum += value;
                }
            }

            // 算出总的能量值，存储
            this.sum = sum;
            return maxValueInfo;
        }

        public string ExchangeTwoDayCourse(int groupClass, int day1, int period1, int day2, int period2)
        {
            string course = "";
            foreach (Group @group in CompareList[groupClass].List)
            {
                if (group.Curriculum.table[day1][period1].CourseName == group.Curriculum.table[day2][period2].CourseName)
                {
                    return course;
                }

                var courseTemp = group.Curriculum.table[day1][period1];
                group.Curriculum.table[day1][period1] = group.Curriculum.table[day2][period2];
                group.Curriculum.table[day2][period2] = courseTemp;

                // positionClass数据不能交换，需要进行还原
                int temp = group.Curriculum.table[day1][period1].PositionClass;
                group.Curriculum.table[day1][period1].PositionClass = group.Curriculum.table[day2][period2].PositionClass;
                group.Curriculum.table[day2][period2].PositionClass = temp;

                course = group.Curriculum.table[day1][period1].CourseName.Substring(0, 2);
                course += group.Curriculum.table[day1][period1].PositionClass;

                // 计算everydayCourseCount的值
                group.Curriculum.RefreshEverydayCourseCount();
            }
            return course;
        }

        
        public bool ExchangeTwoDayCourseForClassified
            (int groupClass, int day1, int period1, int day2, int period2,string class1,string class2)
        {
            int i = 0;
            foreach (GroupList list in CompareList)
            {
                // 遍历交换每个分组的相对应的课程
                if (list.List[0].Curriculum.table[day1][period1].Class == class1
                    && list.List[0].Curriculum.table[day2][period2].Class == class2)
                {
                    var courseTemp = list.List[0].Curriculum.table[day1][period1];

                    list.List[0].Curriculum.table[day1][period1] = list.List[0].Curriculum.table[day2][period2];
                    list.List[0].Curriculum.table[day2][period2] = courseTemp;
                    i++;
                    //Console.WriteLine(class1+class2+"交换了"+i+"次");
                }
            }
            return true;
        }

        internal int GetSpecificPositionCourseAmount(int position, string course)
        {
            return this[position / CourseNumADay][position % CourseNumADay][course].Count;
        }
    }
}
